if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_cs_EventoDestacado_cs_Evento]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)
ALTER TABLE [dbo].[cs_EventoDestacado] DROP CONSTRAINT FK_cs_EventoDestacado_cs_Evento
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[cs_evento_destacado_GetAllByVisibilidad]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[cs_evento_destacado_GetAllByVisibilidad]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[cs_evento_destacado_spAgregar]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[cs_evento_destacado_spAgregar]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[cs_evento_destacado_spGetAll]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[cs_evento_destacado_spGetAll]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[cs_evento_destacado_spGetAllPerm]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[cs_evento_destacado_spGetAllPerm]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[cs_evento_destacado_spOrden]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[cs_evento_destacado_spOrden]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[cs_evento_destacado_spQuitar]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[cs_evento_destacado_spQuitar]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[cs_evento_spDelete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[cs_evento_spDelete]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[cs_evento_spGetAll]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[cs_evento_spGetAll]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[cs_evento_spGetAllByFotografo]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[cs_evento_spGetAllByFotografo]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[cs_evento_spGetAllByNombrePerm]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[cs_evento_spGetAllByNombrePerm]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[cs_evento_spGetAllByUserEvent]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[cs_evento_spGetAllByUserEvent]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[cs_evento_spGetAllByUserFotografoPerm]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[cs_evento_spGetAllByUserFotografoPerm]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[cs_evento_spGetOne]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[cs_evento_spGetOne]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[cs_evento_spInsert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[cs_evento_spInsert]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[cs_evento_spUpdate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[cs_evento_spUpdate]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[cs_evento_user_spDelete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[cs_evento_user_spDelete]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[cs_evento_user_spDeleteByEvento]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[cs_evento_user_spDeleteByEvento]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[cs_evento_user_spInsert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[cs_evento_user_spInsert]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[cs_evento_user_spUsersInEvent]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[cs_evento_user_spUsersInEvent]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[vw_Destacado]') and OBJECTPROPERTY(id, N'IsView') = 1)
drop view [dbo].[vw_Destacado]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[vw_evento]') and OBJECTPROPERTY(id, N'IsView') = 1)
drop view [dbo].[vw_evento]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[vw_fotografo_evento]') and OBJECTPROPERTY(id, N'IsView') = 1)
drop view [dbo].[vw_fotografo_evento]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[vw_user_evento]') and OBJECTPROPERTY(id, N'IsView') = 1)
drop view [dbo].[vw_user_evento]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[cs_Evento]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[cs_Evento]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[cs_EventoDestacado]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[cs_EventoDestacado]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[cs_EventoUserEvento]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[cs_EventoUserEvento]
GO

CREATE TABLE [dbo].[cs_Evento] (
	[EventoID] [bigint] IDENTITY (1, 1) NOT NULL ,
	[Nombre] [nvarchar] (256) COLLATE Modern_Spanish_CI_AS NULL ,
	[Descripcion] [nvarchar] (2000) COLLATE Modern_Spanish_CI_AS NULL ,
	[Visibilidad] [int] NULL ,
	[Estado] [bit] NULL ,
	[FechaCreacion] [datetime] NULL ,
	[FechaModificado] [datetime] NULL ,
	[Clave] [varchar] (10) COLLATE Modern_Spanish_CI_AS NULL ,
	[Tapa] [nvarchar] (256) COLLATE Modern_Spanish_CI_AS NULL ,
	[Fecha] [datetime] NULL ,
	[Precio] [money] NULL ,
	[Fotografo] [nvarchar] (256) COLLATE Modern_Spanish_CI_AS NULL 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[cs_EventoDestacado] (
	[DestacadoID] [bigint] IDENTITY (1, 1) NOT NULL ,
	[EventoID] [bigint] NOT NULL ,
	[NroOrden] [int] NULL 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[cs_EventoUserEvento] (
	[EventoID] [bigint] NULL ,
	[UserID] [nvarchar] (256) COLLATE Modern_Spanish_CI_AS NULL 
) ON [PRIMARY]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


--
-- Definition for view vw_Destacado :
--

CREATE VIEW dbo.vw_Destacado
AS
     select cs_Evento.EventoID,
            cs_Evento.Nombre,
            cs_Evento.Descripcion,
            cs_Evento.Visibilidad,
            cs_Evento.Estado,
            cs_Evento.FechaCreacion,
            cs_Evento.FechaModificado,
            cs_Evento.Clave,
            cs_Evento.Tapa,
            cs_Evento.Fecha,
            cs_Evento.Precio,
            cs_Evento.Fotografo,
            Destacado  = case ISNULL(cs_EventoDestacado.DestacadoID,0) when 0 then 0 else 1 end,
            ISNULL(cs_EventoDestacado.NroOrden, 0) as Orden
     from cs_EventoDestacado inner join cs_Evento
     on cs_EventoDestacado.EventoID = cs_Evento.EventoID

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


--
-- Definition for view vw_evento :
--

CREATE VIEW dbo.vw_evento
AS
     select cs_Evento.EventoID,
            cs_Evento.Nombre,
            cs_Evento.Descripcion,
            cs_Evento.Visibilidad,
            cs_Evento.Estado,
            cs_Evento.FechaCreacion,
            cs_Evento.FechaModificado,
            cs_Evento.Clave,
            cs_Evento.Tapa,
            cs_Evento.Fecha,
            cs_Evento.Precio,
            cs_Evento.Fotografo,
            Destacado  = case ISNULL(cs_EventoDestacado.DestacadoID,0) when 0 then 0 else 1 end,
            ISNULL(cs_EventoDestacado.NroOrden, 0) as Orden
     from cs_Evento left join cs_EventoDestacado
     on cs_Evento.EventoID = cs_EventoDestacado.EventoID

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


--
-- Definition for view vw_fotografo_evento :
--

CREATE VIEW dbo.vw_fotografo_evento
AS
     select cs_Evento.EventoID,
            cs_Evento.Nombre,
            cs_Evento.Descripcion,
            cs_Evento.Visibilidad,
            cs_Evento.Estado,
            cs_Evento.FechaCreacion,
            cs_Evento.FechaModificado,
            cs_Evento.Clave,
            cs_Evento.Tapa,
            cs_Evento.Fecha,
            cs_Evento.Precio,
            cs_Evento.Fotografo,
            Destacado  = case ISNULL(cs_EventoDestacado.DestacadoID,0) when 0 then 0 else 1 end,
            ISNULL(cs_EventoDestacado.NroOrden, 0) as Orden
     from cs_Evento left join cs_EventoDestacado
     on cs_Evento.EventoID = cs_EventoDestacado.EventoID

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


--
-- Definition for view vw_user_evento :
--

--
-- Definition for view vw_user_evento :
--

CREATE VIEW dbo.vw_user_evento
AS
     select cs_Evento.EventoID,
            cs_Evento.Nombre,
            cs_Evento.Descripcion,
            cs_Evento.Visibilidad,
            cs_Evento.Estado,
            cs_Evento.FechaCreacion,
            cs_Evento.FechaModificado,
            cs_Evento.Clave,
            cs_Evento.Tapa,
            cs_Evento.Fecha,
            cs_Evento.Precio,
            cs_Evento.Fotografo,
            cs_EventoUserEvento.UserID as UserEvento,
            Destacado  = case ISNULL(cs_EventoDestacado.DestacadoID,0) when 0 then 0 else 1 end,
            ISNULL(cs_EventoDestacado.NroOrden, 0) as Orden
     from cs_Evento
     inner join cs_EventoUserEvento
     on cs_Evento.EventoID = cs_EventoUserEvento.EventoID
     left join cs_EventoDestacado
     on cs_Evento.EventoID = cs_EventoDestacado.EventoID

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


--
-- Definition for stored procedure cs_evento_destacado_GetAllByVisibilidad :
--

CREATE PROCEDURE dbo.cs_evento_destacado_GetAllByVisibilidad
@visibilidad int
AS
BEGIN
     select vw_Destacado.EventoID,
            vw_Destacado.Nombre,
            vw_Destacado.Descripcion,
            vw_Destacado.Visibilidad,
            vw_Destacado.Estado,
            vw_Destacado.FechaCreacion,
            vw_Destacado.FechaModificado,
            vw_Destacado.Clave,
            vw_Destacado.Tapa,
            vw_Destacado.Fecha,
            vw_Destacado.Precio,
            vw_Destacado.Fotografo,
            vw_Destacado.Destacado
     from vw_Destacado
     where vw_Destacado.Visibilidad = @visibilidad

END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


--
-- Definition for stored procedure cs_evento_destacado_spAgregar :
--

CREATE PROCEDURE dbo.cs_evento_destacado_spAgregar
@eventoId bigint
AS
BEGIN
  if(not exists(select EventoID from cs_EventoDestacado where EventoID=@eventoId))
  begin
       insert into cs_EventoDestacado(EventoID, NroOrden) values(@eventoId,0)
  end
END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


--
-- Definition for stored procedure cs_evento_destacado_spGetAll :
--

CREATE PROCEDURE dbo.cs_evento_destacado_spGetAll
AS
BEGIN
     select vw_Destacado.EventoID,
            vw_Destacado.Nombre,
            vw_Destacado.Descripcion,
            vw_Destacado.Visibilidad,
            vw_Destacado.Estado,
            vw_Destacado.FechaCreacion,
            vw_Destacado.FechaModificado,
            vw_Destacado.Clave,
            vw_Destacado.Tapa,
            vw_Destacado.Fecha,
            vw_Destacado.Precio,
            vw_Destacado.Fotografo,
            vw_Destacado.Destacado,
            vw_Destacado.Orden
     from vw_Destacado
     order by vw_Destacado.Orden
END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE dbo.cs_evento_destacado_spGetAllPerm
@permiso int
AS
BEGIN
     select vw_Destacado.EventoID,
            vw_Destacado.Nombre,
            vw_Destacado.Descripcion,
            vw_Destacado.Visibilidad,
            vw_Destacado.Estado,
            vw_Destacado.FechaCreacion,
            vw_Destacado.FechaModificado,
            vw_Destacado.Clave,
            vw_Destacado.Tapa,
            vw_Destacado.Fecha,
            vw_Destacado.Precio,
            vw_Destacado.Fotografo,
            vw_Destacado.Destacado,
            vw_Destacado.Orden
     from vw_Destacado
     where
     (@permiso=123 and
     (vw_Destacado.Visibilidad = 1
     or vw_Destacado.Visibilidad = 2
     or vw_Destacado.Visibilidad = 3))
     or
     (@permiso=13 and
     (vw_Destacado.Visibilidad = 1
     or vw_Destacado.Visibilidad = 3))
     or
     (@permiso=1 and
     vw_Destacado.Visibilidad = 1)
order by vw_Destacado.Orden 

END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


--
-- Definition for stored procedure cs_evento_destacado_spOrden :
--

CREATE PROCEDURE dbo.cs_evento_destacado_spOrden
@eventoId bigint,
@orden int
AS
BEGIN
  if(exists(select EventoID from cs_EventoDestacado where EventoID=@eventoId))
  begin
       update cs_EventoDestacado set NroOrden = @orden where EventoID=@eventoId
  end
END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


--
-- Definition for stored procedure cs_evento_destacado_spQuitar :
--

CREATE PROCEDURE dbo.cs_evento_destacado_spQuitar
@eventoId bigint
AS
BEGIN
  delete from cs_EventoDestacado where EventoID = @eventoId
END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


--
-- Definition for stored procedure cs_evento_spDelete :
--

CREATE PROCEDURE dbo.cs_evento_spDelete
@eventId bigint
AS
BEGIN
  delete from cs_Evento where EventoID = @eventId
  delete from cs_EventoUserEvento where EventoID = @eventId
END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


--
-- Definition for stored procedure cs_evento_spGetAll :
--

CREATE PROCEDURE dbo.cs_evento_spGetAll
AS
BEGIN
     select vw_evento.EventoID,
            vw_evento.Nombre,
            vw_evento.Descripcion,
            vw_evento.Visibilidad,
            vw_evento.Estado,
            vw_evento.FechaCreacion,
            vw_evento.FechaModificado,
            vw_evento.Clave,
            vw_evento.Tapa,
            vw_evento.Fecha,
            vw_evento.Precio,
            vw_evento.Fotografo,
            vw_evento.Destacado,
            vw_evento.Orden
     from vw_evento
END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


--
-- Definition for stored procedure cs_evento_spGetAllByFotografo :
--

CREATE PROCEDURE dbo.cs_evento_spGetAllByFotografo
@userId varchar(256)
AS
BEGIN
     select vw_evento.EventoID,
            vw_evento.Nombre,
            vw_evento.Descripcion,
            vw_evento.Visibilidad,
            vw_evento.Estado,
            vw_evento.FechaCreacion,
            vw_evento.FechaModificado,
            vw_evento.Clave,
            vw_evento.Tapa,
            vw_evento.Fecha,
            vw_evento.Precio,
            vw_evento.Fotografo,
            vw_evento.Destacado,
            vw_evento.Orden
     from vw_evento
     where vw_evento.Fotografo = @userId
END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


--
-- Definition for stored procedure cs_evento_spGetAllByNombrePerm :
--

CREATE PROCEDURE [dbo].cs_evento_spGetAllByNombrePerm
@nombre nvarchar(256),
@permiso int
AS
BEGIN
     select vw_evento.EventoID,
            vw_evento.Nombre,
            vw_evento.Descripcion,
            vw_evento.Visibilidad,
            vw_evento.Estado,
            vw_evento.FechaCreacion,
            vw_evento.FechaModificado,
            vw_evento.Clave,
            vw_evento.Tapa,
            vw_evento.Fecha,
            vw_evento.Precio,
            vw_evento.Fotografo,
            vw_evento.Destacado,
            vw_evento.Orden
     from vw_evento
     where vw_evento.Nombre like '%'+@nombre+'%'
     and
     ((@permiso=123 and
     (vw_evento.Visibilidad = 1
     or vw_evento.Visibilidad = 2
     or vw_evento.Visibilidad = 3))
     or
     (@permiso=13 and
     (vw_evento.Visibilidad = 1
     or vw_evento.Visibilidad = 3))
     or
     (@permiso=1 and
     vw_evento.Visibilidad = 1))
     order by vw_evento.Orden
END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


--
-- Definition for stored procedure cs_evento_spGetAllByUserEvent :
--

--
-- Definition for stored procedure cs_evento_spGetAllByUserEvent :
--

CREATE PROCEDURE dbo.cs_evento_spGetAllByUserEvent
@userId varchar(256)
AS
BEGIN
     select vw_user_evento.EventoID,
            vw_user_evento.Nombre,
            vw_user_evento.Descripcion,
            vw_user_evento.Visibilidad,
            vw_user_evento.Estado,
            vw_user_evento.FechaCreacion,
            vw_user_evento.FechaModificado,
            vw_user_evento.Clave,
            vw_user_evento.Tapa,
            vw_user_evento.Fecha,
            vw_user_evento.Precio,
            vw_user_evento.Fotografo,
            vw_user_evento.Destacado,
            vw_user_evento.Orden
     from vw_user_evento
     where vw_user_evento.UserEvento = @userId
END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


--
-- Definition for stored procedure cs_evento_spGetAllByUserFotografoPerm :
--

CREATE PROCEDURE dbo.cs_evento_spGetAllByUserFotografoPerm
@userFotografo varchar(256),
@permiso int
AS
BEGIN
     select vw_evento.EventoID,
            vw_evento.Nombre,
            vw_evento.Descripcion,
            vw_evento.Visibilidad,
            vw_evento.Estado,
            vw_evento.FechaCreacion,
            vw_evento.FechaModificado,
            vw_evento.Clave,
            vw_evento.Tapa,
            vw_evento.Fecha,
            vw_evento.Precio,
            vw_evento.Fotografo,
            vw_evento.Destacado,
            vw_evento.Orden
     from vw_evento
     where vw_evento.Fotografo = @userFotografo
     and (
     (@permiso=123 and
     (vw_evento.Visibilidad = 1
     or vw_evento.Visibilidad = 2
     or vw_evento.Visibilidad = 3))
     or
     (@permiso=13 and
     (vw_evento.Visibilidad = 1
     or vw_evento.Visibilidad = 3))
     or
     (@permiso=1 and vw_evento.Visibilidad = 1)
     )


END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


--
-- Definition for stored procedure cs_evento_spGetOne :
--

--
-- Definition for stored procedure cs_evento_spGetOne :
--

create PROCEDURE [dbo].[cs_evento_spGetOne]
@id Bigint
AS
BEGIN
     select EventoID,
            Nombre,
            Descripcion,
            Visibilidad,
            Estado,
            FechaCreacion,
            FechaModificado,
            Clave,
            Tapa,
            Fecha,
            Precio,
            Fotografo
     from cs_Evento
     WHERE EventoID = @id
END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


--
-- Definition for stored procedure cs_evento_spInsert :
--

CREATE PROCEDURE dbo.cs_evento_spInsert
@nombre nvarchar(256),
@descripcion nvarchar(2000),
@visibilidad int,
@precio money,
@fecha datetime,
@fotografo nvarchar(256)
AS
BEGIN

     if(exists(select Nombre from cs_Evento where Nombre=@nombre))
        return 0

     declare @clave varchar(10), @random varchar(500)

     set @Clave = REPLACE(substring(CONVERT(varchar(500),newid()),1,11),'-', '')

     insert into cs_Evento(
     Nombre,
     Descripcion,
     Visibilidad,
     Precio,
     Fecha,
     Fotografo,
     FechaCreacion,
     Estado,
     Clave)
     values(
     @nombre,
     @descripcion,
     @visibilidad,
     @precio,
     @fecha,
     @fotografo,
     getdate(),
     1,
     @Clave)

     IF @@ERROR <> 0
     begin
		SELECT @@ERROR
		return
	 end
     select @@Identity        		

END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


--
-- Definition for stored procedure cs_evento_spUpdate :
--

--
-- Definition for stored procedure cs_evento_spUpdate :
--

CREATE PROCEDURE dbo.cs_evento_spUpdate
@eventoId bigint,
@nombre nvarchar(256),
@descripcion nvarchar(2000),
@visibilidad int,
@precio money,
@fecha datetime,
@fotografo nvarchar(256),
@tapa nvarchar(256)
AS
BEGIN
     update cs_Evento
     set Nombre=@nombre,
     Descripcion=@descripcion,
     Visibilidad=@visibilidad,
     FechaModificado=getdate(),
     Fecha=@fecha,
     Precio=@precio,
     Fotografo=@fotografo,
     Tapa=@tapa
     where EventoID=@eventoId
END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


--
-- Definition for stored procedure cs_evento_user_spDelete :
--

--
-- Definition for stored procedure cs_evento_user_spDelUsersToEvent :
--

CREATE  PROCEDURE dbo.cs_evento_user_spDelete
@eventoId bigint,
@userName varchar(256)
AS
BEGIN
     delete from cs_EventoUserEvento
     where EventoID = @eventoId  and UserId = @userName
END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


--
-- Definition for stored procedure cs_evento_user_spDeleteByEvento :
--

CREATE  PROCEDURE dbo.cs_evento_user_spDeleteByEvento
@eventoId bigint
AS
BEGIN
     delete from cs_EventoUserEvento
     where EventoID = @eventoId
END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


--
-- Definition for stored procedure cs_evento_user_spInsert :
--

--
-- Definition for stored procedure cs_evento_user_spInsert :
--

CREATE   PROCEDURE dbo.cs_evento_user_spInsert
@eventoId bigint,
@userName varchar(256)
AS
BEGIN
if(not exists(select EventoID, UserID from cs_EventoUserEvento where EventoID = @eventoId  and UserId = @userName))
begin
  insert into cs_EventoUserEvento(EventoID, UserID)
  values(@eventoId, @userName)
end
END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


--
-- Definition for stored procedure cs_evento_user_spUsersInEvent :
--

CREATE PROCEDURE dbo.cs_evento_user_spUsersInEvent
@eventoId bigint
AS
BEGIN
  select UserID from cs_EventoUserEvento
  where EventoID=@eventoId
END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

